module iTimer.io.Stream ;

import iTimer.Types;

alias tango.io.device.Conduit.InputStream InputStream;
scope final class vInputStream :  InputStream {
	alias tango.io.model.IConduit.IOStream.Eof  Eof ;
	private vBuffer bu ;
	private size_t	pos ;
	
	this (vBuffer _bu){
		bu	= _bu ;
	}
	
	long seek(long offset, Anchor anchor = Anchor.Begin) {
        	throw new IOException("ZlibInput does not support seek requests");
	}
	
	void close () {
		bu	= null ;
	}
	
	tango.io.model.IConduit.IConduit conduit(){
		assert(false);
		return null;
	}
	
	InputStream flush (){
		return this ;
	}
	
	InputStream input (){
		return this ;
	}
	
	size_t read (void[] dst) {
		void[] tmp	= bu.slice[ pos .. $ ] ;
		size_t len ;
		if( dst.length >= tmp.length ) {
			len	= tmp.length ;
		} else {
			len	= dst.length ;	
		}
		tango.stdc.string.memcpy(&dst[0], &tmp[0], len);
		pos	+= len ;
		return len ;
	}
	
	void[] load (size_t max = -1) {
		if( max < 0 ){
			max	= 0 ;
		}
		void[] tmp	= bu.slice[ pos .. $ ] ;
		if( max > bu.length ){
			max	= bu.length ;
		}
		pos	+= max ;
		return tmp[0..max] ;
	}
}